package com.xzit.headline.adapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.xzit.headline.bean.ResponseData;
import com.xzit.headline.fragment.sub.NewListFragment;
import com.xzit.headline.fragment.sub.Page1Fragment;

import java.util.ArrayList;
import java.util.List;

/**
 * 给首页设置适配器
 */
public class MyNewPagerAdapter extends FragmentPagerAdapter {
    //集合可以让我们重用Fragment
    private List<ResponseData.DataBean> datas = new ArrayList<>();

    public List<ResponseData.DataBean> getDatas() {
        return datas;
    }

    private List<Fragment> fragments = new ArrayList<>();

    public MyNewPagerAdapter(@NonNull FragmentManager fm, List<ResponseData.DataBean> list) {
        super(fm);
        datas.addAll(list);//将一个集合中所有数据加入


        //默认情况页面是假的页面,现在换成新闻的列表页面
        for (ResponseData.DataBean item : datas) {
            fragments.add(new NewListFragment(item.url));
        }
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return datas.get(position).title;//将标题返回
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return datas.size();
    }
}
